/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.OutStorageDetailDao;
import com.sigmatrix.h5.dto.OutStorageDetailData;
import com.sigmatrix.h5.dto.ProductData;
import com.sigmatrix.h5.entity.OutStorageDetail;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.service.OutStorageDetailService;
import com.sigmatrix.h5.service.ProductService;
import com.sigmatrix.h5.service.common.impl.BaseServiceImpl;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.vo.OutStorageDetailVo;

/**
 * @ClassName: OutStorageDetailServiceImpl
 * @Description: 出库单详情业务层接口
 * @author youjun @date 2017-06-24 17:35:21
 */
@Service(value = "outStorageDetailService")
public class OutStorageDetailServiceImpl extends BaseServiceImpl<OutStorageDetailData>
		implements OutStorageDetailService {
	
	/** 出库单详情数据库操作接口 */
	@Autowired
	private transient OutStorageDetailDao outStorageDetailDao;
	
	/**产品业务层接口*/
	@Autowired
	private transient ProductService productService;

	/**
	 * (non-Javadoc)
	 *@Title: findById
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.OutStorageDetailService#findById(java.lang.Integer)
	 *@author youjun
	 *@date 2017年6月26日 下午3:07:03
	 */
	@Override
	public OutStorageDetailData findById(final Integer id, final String enterpriseCode) throws ServiceException {
		if (id == null) {
			throw new ServiceException("请指定主键");
		}

		final OutStorageDetailData data = new OutStorageDetailData();
		try {
			final OutStorageDetail outStorageDetail = outStorageDetailDao.findById(id, enterpriseCode);
			if (outStorageDetail != null) {
				BeanUtils.copyProperties(outStorageDetail, data);

			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return data;
	}

	/**
	 * (non-Javadoc)
	 *@Title: saveOrUpdate
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.OutStorageDetailService#saveOrUpdate(com.sigmatrix.sc.handheld.inter.web.vo.OutStorageDetailVo, java.util.Map)
	 *@author youjun
	 *@date 2017年6月26日 下午3:06:59
	 */
	@Override
	public int saveOrUpdate(final OutStorageDetailVo outStorageDetailVo, final Map<String, Object> map)
			throws ServiceException {

		if (outStorageDetailVo == null) {
			throw new ServiceException("");
		}
		final String enterpriseCode = outStorageDetailVo.getEnterpriseCode();
		OutStorageDetail outStorageDetail = new OutStorageDetail();
		int result = 0;

		try {
			if (outStorageDetailVo.getId() == null) {
				BeanUtils.copyProperties(outStorageDetailVo, outStorageDetail);
				outStorageDetail.setCreatorId(getUserId(map));
				outStorageDetail.setCreateTime(new Date());
				if (outStorageDetail.getStatus() == null) {
					outStorageDetail.setStatus(Cons.STATUS_ENABLE);
				}
				result = outStorageDetailDao.save(outStorageDetail, enterpriseCode);
			} else {
				outStorageDetail = outStorageDetailDao.findById(outStorageDetailVo.getId(), enterpriseCode);
//				outStorageDetail.setOutStorageId(outStorageDetailVo.getOutStorageId());
//				outStorageDetail.setWorkId(outStorageDetailVo.getWorkId());

				result = outStorageDetailDao.update(outStorageDetail, enterpriseCode);
			}
			return result;
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	/**
	 * (non-Javadoc)
	 *@Title: findPartColumnListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.OutStorageDetailService#findPartColumnListByCondition(com.sigmatrix.sc.handheld.inter.web.vo.OutStorageDetailVo)
	 *@author youjun
	 *@date 2017年6月26日 下午3:06:53
	 */
	@Override
	public List<OutStorageDetailData> findPartColumnListByCondition(final OutStorageDetailVo outStorageDetailVo) 
			throws ServiceException {
		
		super.validateBasicParamsThrow(outStorageDetailVo, false, false);
		if(outStorageDetailVo==null) {
			throw new ServiceException("参数不能为空");
		}
		final OutStorageDetail outStorageDetail = new OutStorageDetail();
		if (outStorageDetailVo != null) {
			BeanUtils.copyProperties(outStorageDetailVo, outStorageDetail);
		}
		try {
			final List<OutStorageDetailData> dataList = outStorageDetailDao.findPartColumnListByCondition(outStorageDetail, outStorageDetailVo.getEnterpriseCode());
			// 填充产品信息
			fillProductInfo(dataList, outStorageDetailVo.getEnterpriseCode());
			return dataList;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	
	/**
	 *@Title: fillProductInfo
	 *@Description: 填充产品信息
	 *@return void
	 *@author youjun
	 *@date 2017年6月26日 下午2:51:51
	 *@param dataList
	 * @throws ServiceException 
	 */
	private void fillProductInfo(final List<OutStorageDetailData> dataList, final String enterpriseCode) throws ServiceException {
		
		if(!CollectionUtils.isEmpty(dataList)) {
			ProductData product = null;
			final Map<Integer, ProductData> productMap = productService.getAllProductMap(enterpriseCode);
			for(final OutStorageDetailData data:dataList) {
				product = productService.getProductFromMap(productMap, data.getProductId(), enterpriseCode);
				if(product!=null) {
					data.setProductName(product.getProductName());
					data.setPackStandard(product.getPackStandard());
					data.setProductUnit(product.getProductUnit());
				}
			}
		}
	}

	/**
	 * (non-Javadoc)
	 *@Title: findListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.OutStorageDetailService#findListByCondition(com.sigmatrix.sc.handheld.inter.web.vo.OutStorageDetailVo)
	 *@author youjun
	 *@date 2017年6月26日 下午3:07:11
	 */
	@Override
	public List<OutStorageDetailData> findListByCondition(final OutStorageDetailVo outStorageDetailVo)
			throws ServiceException {

		final List<OutStorageDetailData> dataList = new ArrayList<OutStorageDetailData>();
		try {
			final OutStorageDetail outStorageDetail = new OutStorageDetail();
			if (outStorageDetailVo != null) {
				BeanUtils.copyProperties(outStorageDetailVo, outStorageDetail);
			}
			final List<OutStorageDetail> list = outStorageDetailDao.findListByCondition(outStorageDetail, outStorageDetailVo.getEnterpriseCode());
			if (!CollectionUtils.isEmpty(list)) {
				OutStorageDetailData data = null;
				for (final OutStorageDetail item : list) {
					data = new OutStorageDetailData();
					BeanUtils.copyProperties(item, data);
					dataList.add(data);
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return dataList;
	}

}
